/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import java.sql.*;
import java.sql.Connection;
import java.util.Vector;

/**
 *
 * @author user
 */
public class Customer 
{
    private static String CusstomerName;
    private int customerid;
    private String customername;
    private String address;
    private String phone;
    private String email;
    private String gender;
    private String customertype;
    private String Description;

    /**
     * @return the customerid
     */
    public int getCustomerid() {
        return customerid;
    }

    /**
     * @param customerid the customerid to set
     */
    public void setCustomerid(int customerid) {
        this.customerid = customerid;
    }

    /**
     * @return the customername
     */
    public String getCustomername() {
        return customername;
    }

    /**
     * @param customername the customername to set
     */
    public void setCustomername(String customername) {
        this.customername = customername;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }


    /**
     * @return the Description
     */
    public String getDescription() {
        return Description;
    }

    /**
     * @param Description the Description to set
     */
    public void setDescription(String Description) {
        this.Description = Description;
    }
    
    
    public Vector<Customer> customerlist()
    {
        Vector<Customer> custlist=new Vector<Customer>();
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="select* from Customer";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
            while(rs.next())
            {
                Customer ct=new Customer();
                ct.setCustomerid(rs.getInt("CustomerId"));
                ct.setCustomername(rs.getString("CusstomerName"));
                ct.setAddress(rs.getString("Address"));
                ct.setPhone(rs.getString("Phone"));
                ct.setEmail(rs.getString("Email"));
                ct.setGender(rs.getString("Gender"));
                ct.setCustomertype(rs.getString("CustomerType"));
                ct.setDescription(rs.getString("Description"));
                custlist.add(ct);
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            custlist=null;
        }
        return custlist;
    }

    /**
     * @return the customertype
     */
    public String getCustomertype() {
        return customertype;
    }

    /**
     * @param customertype the customertype to set
     */
    public void setCustomertype(String customertype) {
        this.customertype = customertype;
    }
    
   
    public Customer Infomation_Customer(String customername)
    {
        Customer ct=new Customer();
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="select * from Customer where CustomerId=? ";
            PreparedStatement pt=con.prepareCall(query);
            pt.setString(1, customername);
            ResultSet rs=pt.executeQuery();
            while(rs.next())
            {
               
                ct.setCustomername(rs.getString("CusstomerName"));
                ct.setAddress(rs.getString("Address"));
                ct.setPhone(rs.getString("Phone"));
                ct.setEmail(rs.getString("Email"));
                ct.setGender(rs.getString("Gender"));
                ct.setCustomertype(rs.getString("CustomerType"));
                ct.setDescription(rs.getString("Description"));
                ct.setCustomerid(rs.getString("CustomerId"));
               
                
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return ct;
    }
     public Customer Infomation_Customer1(String customername)
    {
        Customer ct=new Customer();
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="select * from Customer where CustomerId=? ";
            PreparedStatement pt=con.prepareCall(query);
            pt.setString(1, customername);
            ResultSet rs=pt.executeQuery();
            while(rs.next())
            {
               
                ct.setCustomername(rs.getString("CusstomerName"));
                ct.setAddress(rs.getString("Address"));
                ct.setPhone(rs.getString("Phone"));
                ct.setEmail(rs.getString("Email"));
                ct.setGender(rs.getString("Gender"));
                ct.setCustomertype(rs.getString("CustomerType"));
                ct.setDescription(rs.getString("Description"));
                ct.setCustomerid(rs.getString("CustomerId"));
            
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return ct;
    }
    
    
    
    
    

    private void setCustomerid(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    public boolean Insert_Customer()
    {
        boolean kq=true;
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="insert into Customer values(?,?,?,?,?,?,?)";
            PreparedStatement pt=con.prepareCall(query);
            pt.setString(1, this.customername);
            pt.setString(2, this.address);
            pt.setString(3, this.phone);
            pt.setString(4, this.email);
            pt.setString(5, this.gender);
            pt.setString(6, this.customertype);
            pt.setString(7, this.Description);
            kq=pt.executeUpdate()>0;
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            return  false;
        }
        return true;
    }
   public boolean editcustomer(int id)
   {
       boolean kq=true;
       try
       {
           Connection con=ConecttoDatabase.getConnection();
           String query="Update  Customer set CusstomerName='"+this.customername+"',Address='"+this.address+"',Phone='"+this.phone+"',Email='"+this.email+"',Gender='"+this.gender+"',CustomerType='"+this.customertype+"',Description='"+this.Description+"' where CustomerId='"+this.customerid+"' ";
           Statement st= con.createStatement();
           kq=st.executeUpdate(query)>0;
       }
       catch(Exception ex)
       {
           System.out.println(ex);
           kq=false;
       }
       return kq;
   }
   

    public static Vector CustumerPT()
    {
        Vector vp = new Vector();
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = "select * from Customer order by CustomerId DESC";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
            while (rs.next()) 
            {
                Vector vt = new Vector();
                vt.add(rs.getInt("CustomerId"));
                vt.add(rs.getString("CusstomerName"));
                vt.add(rs.getString("Address"));
                vt.add(rs.getString("Phone"));
                vt.add(rs.getString("Email"));
                vt.add(rs.getString("Gender"));
                vt.add(rs.getString("CustomerType"));
                vt.add(rs.getString("Description"));
                vp.add(vt);
            }
        } 
        catch (Exception ex) 
        {
            vp = null;
            System.out.println(ex);
        }
        return vp;
    }
    public static  Vector SearchCustumer(String CusstomerName)
    {
        Vector Vender = new  Vector(); 
        try
        {//select * from Vender where VanderName like '%"+this.'
            Connection con = ConecttoDatabase.getConnection();
            String query = "select * from Customer where CusstomerName like '%"+CusstomerName+"%'";
            Statement st=con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
               Vector vt = new Vector();
                vt.add(rs.getInt("CustomerId"));
                vt.add(rs.getString("CusstomerName"));
                vt.add(rs.getString("Address"));
                vt.add(rs.getString("Phone"));
                vt.add(rs.getString("Email"));
                vt.add(rs.getString("Gender"));
                vt.add(rs.getString("CustomerType"));
                vt.add(rs.getString("Description"));
                Vender.add(vt);
            }
        }
        catch(Exception ex)
        {
            Vender=null;
            System.out.println(ex);
        }
        
        return Vender;
    }
   
       public static int Count()
    {
        int co = 0;
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = " select count(*) from Customer";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                co += rs.getInt(1);
            }
        } catch (Exception e) {
           
            System.out.println(e);
        }
        return co;
    }

    public boolean Edit_Customer(Integer valueOf) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 public boolean Delete_Customer(int id)
 {
     boolean kq=true;
     try
     {
         Connection con=ConecttoDatabase.getConnection();
         String query="delete from Customer where CustomerId=?";
         PreparedStatement pt=con.prepareCall(query);
         pt.setInt(1, id);
         kq=pt.executeUpdate()>0;
         
         
     }
     catch(Exception ex)
     {
         System.out.println(ex);
         kq=false;
     }
     return kq;
 }
  public static boolean KTDelete_Customer(String id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query=" select CustomerId from SalesBill where CustomerId='"+id+"' ";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(id.equals(rs.getString("CustomerId")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
         {
             System.out.println(ex);
            
         }
         return kq;
     }
  
           public static boolean KTDelete_Customer1(String id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query=" select CustomerId from CallManagement where CustomerId='"+id+"' ";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(id.equals(rs.getString("CustomerId")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
         {
             System.out.println(ex);
            
         }
         return kq;
     }
           
}
